package br.com.virtual.vender.service.impl;

import java.util.List;
import java.util.TreeSet;

import javax.ejb.Local;
import javax.ejb.Stateless;
import javax.inject.Inject;

import br.com.virtual.cadastrar.model.Produto;
import br.com.virtual.vender.dao.InteresseDAO;
import br.com.virtual.vender.model.Interesse;
import br.com.virtual.vender.model.ItemVenda;
import br.com.virtual.vender.service.InteresseService;

@Stateless
@Local(InteresseService.class)
public class InteresseServiceImpl implements InteresseService {

	@Inject
	private InteresseDAO interesseDAO;

	@Override
	public Interesse create(Interesse interesse) {

		if (interesse.getId() == null)
			return interesseDAO.create(interesse);

		this.update(interesse);
		return interesse;
	}

	@Override
	public Interesse read(Long id) {
		return interesseDAO.read(id);
	}

	@Override
	public Interesse update(Interesse interesse) {
		return interesseDAO.update(interesse);
	}

	@Override
	public void delete(Interesse interesse) {

		interesse = this.read(interesse.getId());
		interesse.setProdutos(new TreeSet<Produto>());
		interesseDAO.update(interesse);
		interesseDAO.delete(interesse);
	}

	@Override
	public List<ItemVenda> getItens(Interesse interesse) {
		return interesseDAO.getItens(interesse);
	}

}
